<?php 

class App_View_Helper_Table extends Zend_View_Helper_Abstract {
	
	protected $_paginator 		= null;
	protected $_columns 		= array();
	protected $_checkAll 		= false;
	protected $_checkAllActions = array();
	protected $_actions			= array();
	
	protected $_request 		= null;
	
	
	public function table($paginator = null) {
		if (null !== $paginator) 
			$this->setPaginator($paginator);
			
		$view = Zend_Layout::getMvcInstance()->getView();
		$view->setScriptPath(APPLICATION_PATH.'/layouts/');	
		
		$this->_request = Zend_Controller_Front::getInstance()->getRequest();
		
		return $this;
	}
	
	public function setPaginator(Zend_Paginator $paginator) {
		$this->_paginator = $paginator;
		return $this;
	}
	
	public function addColumn($key, $label, $attr = array(), $tdattr = array(), $params = array(), $type = 'standard') {
		$this->_columns[$key] = array('type'=>$type, 'label' => $label, 'attr'=>$attr, 'tdattr'=>$tdattr, 'params'=>$params);
		return $this;
	}
	
	public function checkAllColumn($flag = true) {
		$this->_checkAll = $flag;
		return $this;
	}
	
	public function addCheckAllAction($action, $label) {
		$this->_checkAllActions[$action] = $label;
		return $this;
	}
	
	public function addAction($label, $class, $params = array(), $keys) {
		$this->_actions[] = array(
			'label'  => $label,
			'class'  => $class,
			'params' => $params,
			'keys'	 => $keys
		);
		return $this;
	}
	
	public function __toString() {
		
		$this->view
					->assign('paginator', $this->_paginator)
					->assign('columns', $this->_columns)
					->assign('checAll', $this->_checkAll)
					->assign('checAllActions', $this->_checkAllActions)
					->assign('actions', $this->_actions)
					->assign('sort', $this->_request->getParam('sort', 'id'))
					->assign('sortdir', $this->_request->getParam('sortdir', 'desc'))
					;
					
		return $this->view->render('_table.phtml');
		
	}
	
}